package org.ilead.iserve.common;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.ilead.iserve.constants.SessionKeyConstant;
import org.ilead.iserve.model.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 会员登录拦截器抽象类
 * @author jill
 *
 */
public abstract class AbstractLoginInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// 取得请求相关的ActionContext实例
		ActionContext ctx = invocation.getInvocationContext();
		Map session = ctx.getSession();
		// 取出名为user的Session属性
		User user = (User) session.get(SessionKeyConstant.LOGIN_USER);
		
		// 如果没有登陆，都返回重新登陆
		if (user != null && user.getPermission().intValue() >= getPermission()) {
			return invocation.invoke();
		}else if(user != null){
			return Action.INPUT;
		}
		// 没有登陆，将服务器提示设置成一个HttpServletRequest属性
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		String lastAccessPage = request.getContextPath() + request.getServletPath();
		session.put(SessionKeyConstant.LAST_ACCESS_PAGE, lastAccessPage);
		ctx.put("tip", "您还没有登陆，请您登陆，谢谢");
		// 直接返回login的逻辑视图
		return Action.LOGIN;
	}
	
	public abstract int getPermission();

}
